const ajax = {
    get:function(url,callback,data){
        const xhr = new XMLHttpRequest();
        let str;
        if(data){
            str = Object.keys(data).map(item => `${item}=${data[item]}`).join('&');
        }
       
        xhr.open('get',url + '?' + str);
        xhr.send();
        xhr.onreadystatechange = () => {
            if(xhr.readyState === 4){
                callback(xhr.response);
            }
        }
    },
    post:function(url,callback,data){
        const xhr = new XMLHttpRequest();
        const str = Object.keys(data).map(item => `${item}=${data[item]}`).join('&');
        xhr.open('post',url);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send(str);
        xhr.onreadystatechange = () => {
            if(xhr.readyState === 4){
                callback(xhr.response);
            }
        }
    }
}

export default ajax